package com.ruoyi.web.controller.minglvmini;


import com.github.pagehelper.PageInfo;
import com.minglv.mini.domain.Activity;
import com.minglv.mini.domain.ActivityApply;
import com.minglv.mini.domain.dto.ActiveApplyStatusDto;
import com.minglv.mini.domain.dto.ActivityApplyDto;
import com.minglv.mini.domain.dto.ActivityApplyVo;
import com.minglv.mini.service.IActivityApplyService;
import com.minglv.mini.service.IActivityService;
import com.minglv.mini.tools.JSONUtils;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.constant.HttpStatus;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.poi.ExcelUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.util.CollectionUtils;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

/**
 *
 */
@Api(tags = "Amini-活动管理")
@RestController
@RequestMapping("/system/activity")
public class ActivityController extends BaseController {

    @Autowired
    private IActivityService activityService;

    @Resource
    private IActivityApplyService activityApplyService;

    @GetMapping("/applyList")
    @ApiOperation("报名列表")
    public TableDataInfo activeApplyList(@RequestParam(name = "activeId") @ApiParam(value = "活动id") Long activeId,
                                         @RequestParam(name = "searchName", required = false) @ApiParam(value = "搜索内容") String searchName,
                                         @RequestParam(name = "status", required = false) @ApiParam(value = "报名状态，0 待审核，1已报名。2已签到，3，已取消 4已拒绝") Integer status) {
        startPage();
        List<ActivityApplyDto> list = activityApplyService.activeApplyList(activeId, searchName, status);
        TableDataInfo rspData = new TableDataInfo();
        rspData.setCode(HttpStatus.SUCCESS);
        rspData.setMsg("查询成功");
        rspData.setTotal(new PageInfo(list).getTotal());
        if (!CollectionUtils.isEmpty(list)) {
            List<ActivityApplyVo> activityApplyVos = new ArrayList<>();
            for (ActivityApplyDto activityApplyDto : list) {
                ActivityApplyVo activityApplyVo = new ActivityApplyVo();
                BeanUtils.copyProperties(activityApplyDto, activityApplyVo);
                activityApplyVo.setForms(JSONUtils.json2Map(activityApplyDto.getForms()));
                activityApplyVos.add(activityApplyVo);
            }
            rspData.setRows(activityApplyVos);
        }
        return rspData;

    }

    @PostMapping("/activeApplyStatus")
    @ApiOperation("修改报名状态")
    public AjaxResult activeApplyStatus(@Validated @RequestBody ActiveApplyStatusDto aasd) {
        ActivityApply activityApply = activityApplyService.selectActivityApplyById(aasd.getId());
        activityApply.setStatus(aasd.getStatus());
        return toAjax(activityApplyService.updateActivityApply(activityApply));
    }
        /**
     * 查询【请填写功能名称】列表
     */
    @PreAuthorize("@ss.hasPermi('system:activity:list')")
    @GetMapping("/list")
    public TableDataInfo list(Activity activity)
    {
        startPage();
        List<Activity> list = activityService.selectActivityList(activity);
        TableDataInfo rspData = new TableDataInfo();
        rspData.setCode(HttpStatus.SUCCESS);
        rspData.setMsg("查询成功");
        rspData.setTotal(new PageInfo(list).getTotal());
        if (!CollectionUtils.isEmpty(list)) {
            list = list.stream().peek(m -> {
                List<ActivityApplyDto> activityApplyDtos = activityApplyService.activeApplyList(m.getId(), null, null);
                m.setApplyNum(CollectionUtils.isEmpty(activityApplyDtos) ? 0 : activityApplyDtos.size());
            }).collect(Collectors.toList());
            rspData.setRows(list);
        }
        return rspData;
    }

    /**
     * 导出【请填写功能名称】列表
     */
    @PreAuthorize("@ss.hasPermi('system:activity:export')")
    @Log(title = "活动管理", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, Activity activity)
    {
        List<Activity> list = activityService.selectActivityList(activity);
        ExcelUtil<Activity> util = new ExcelUtil<Activity>(Activity.class);
        util.exportExcel(response, list, "【请填写功能名称】数据");
    }

    /**
     * 获取【请填写功能名称】详细信息
     */
    @PreAuthorize("@ss.hasPermi('system:activity:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return success(activityService.selectActivityById(id));
    }

    /**
     * 新增【请填写功能名称】
     */
    @PreAuthorize("@ss.hasPermi('system:activity:add')")
    @Log(title = "活动管理", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody Activity activity)
    {
        return toAjax(activityService.insertActivity(activity));
    }

    /**
     * 修改【请填写功能名称】
     */
    @PreAuthorize("@ss.hasPermi('system:activity:edit')")
    @Log(title = "活动管理", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody Activity activity)
    {
        return toAjax(activityService.updateActivity(activity));
    }

    /**
     * 删除【请填写功能名称】
     */
    @PreAuthorize("@ss.hasPermi('system:activity:remove')")
    @Log(title = "活动管理", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(activityService.deleteActivityByIds(ids));
    }



}
